# Copyright (C) 2001-2003 by Simon Nieuviarts

# Files to compile
FILES=$(wildcard *.S *.c) printf.c sprintf.c doprnt.c panic.c div64.c strtol.c strtoul.c string.c console.c
DIRS=. ../shared

# crt0.o must be the first object linked
OBJS=$(strip crt0.o $(filter-out crt0.o,$(notdir $(patsubst %.S,%.o,$(patsubst %.c,%.o,$(FILES))))))

CC=gcc -m32
AS=gcc -m32
LD=ld -melf_i386

KFLAGS=-Wall -g -gstabs -pipe -std=c99 -nostdinc -fno-stack-protector
#KFLAGS=-Wall -O2 -fomit-frame-pointer -pipe -std=c99 -nostdinc

DEF=-D__KERNEL__
INC=$(patsubst %,-I%,$(DIRS))
CCL=$(CC) -Werror $(KFLAGS) $(DEF) $(INC)
ASL=$(AS) -DASSEMBLER $(DEF) $(INC)
DEPS=$(patsubst %.o,%.d,$(OBJS))

# Where to search for source files
vpath %.c $(DIRS)
vpath %.h $(DIRS)
vpath %.S $(DIRS)

# Targets that are not files
.PHONY: clean all kbd_linux

# The default target
# Sous MacOS, changer la destination en ~/Library/VirtualBox/TFTP/
all: kbd_linux kernel.bin
	/bin/cp -f kernel.bin ~/.VirtualBox/TFTP/

kbd_linux:
	$(MAKE) -C kbd_linux

# Automatic rules to build dependency files
%.d: %.c
	@$(SHELL) -ec '$(CCL) -M $< | sed '\''s|\($*\)\.o[ :]*|\1.o $@ : Makefile |g'\'' > $@; [ -s $@ ] || rm -f $@'

%.d: %.S
	@$(SHELL) -ec '$(ASL) -M $< | sed '\''s|\($*\)\.o[ :]*|\1.o $@ : Makefile |g'\'' > $@; [ -s $@ ] || rm -f $@'

# Build and include dependency files
-include $(DEPS)

# Automatic rules to build the object files
%.o: %.S
	$(ASL) -c $< -o $@

%.o: %.c
	$(CCL) -c $< -o $@

%.s: %.c
	$(CCL) -S -c $< -o $@

# A text screen for exception dump
task_dump_screen.o: empty.o task_dump_screen.txt
	objcopy empty.o --add-section=.task_dump_screen=task_dump_screen.txt --set-section-flags=.task_dump_screen=contents,alloc,load,data task_dump_screen.o

# Generate the kernel, ready to run
kernel.bin: kernel.lds $(OBJS) kbd_linux/kbd_linux.o task_dump_screen.o
	$(LD) -e entry -Tkernel.lds $(OBJS) kbd_linux/kbd_linux.o task_dump_screen.o -o $@

clean:
	make -C kbd_linux clean
	rm -f $(OBJS) $(DEPS) kernel.bin task_dump_screen.o
